<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/29
 * Time: 11:04
 */

namespace app\index\model;
use think\Model;
use think\Db;

class Order extends Model
{
    public function OrderGoods()
    {
        return $this->hasMany('OrderGoods','order_id','id');
    }

    public function Shop(){
        return $this->hasOne('Shop','id','shop_id');
    }

    public function salesman($out_trade_no){
        $order = Db::name('order')->where('order_number',$out_trade_no)->find();//订单所有数据
        $manage_id = Db::name('share_member')->where('member_id',$order['member_id'])->value('manage_id');//获得直属业务员
        if(!empty($manage_id)){
            $salesman_array = parentPath($manage_id,'salesman',true);//当前业务员上级
            $salesman_list = array_reverse($salesman_array);
            // $salesman_list = array_slice($salesman_array,0,3);
            if($salesman_list){
                $pre = 0;
                foreach ($salesman_list as $k => $v){
                    $amount = Db::name('order')->where(['order_number|shop_order_number' => $out_trade_no])->sum('amount');//订单总额
                    if($k > 2) break;
                    $zhishu = Db::name('manager')->where('id',$v['manage_id'])->value('per');//直接销售比~
                    if(is_null($zhishu) || $k != 0) $zhishu = 0;

                    if($k == 0)  $pre = Db::name('config')->where('varname','web_d'.($k+1))->value('varvalue');
                    if($k == 1)  $pre = Db::name('config')->where('varname','web_d'.($k+1))->value('varvalue');
                    if($k == 2)  $pre = Db::name('config')->where('varname','web_d'.($k+1))->value('varvalue');

                    $single_per = 0;
                    $per_order = Db::name('order')->where(['order_number|shop_order_number' => $out_trade_no])->group('order_number')->select();//~订单列用于商品列表 没必要2次循环 一次就查出多单数据 否则总单会多次循环
                    //$alipaySevice->writeLog('$per_order'.$v['manage_id'].'-'.json_encode($per_order));
                    foreach ($per_order as $ko => $vo){
                        if($vo['order_type'] == 1){ //商品
                            $order_goods =  Db::name('order_goods')->where(['order_number|shop_order_number' => $out_trade_no])->select();
                            foreach ($order_goods as $ko1 => $vo1){
                                $s_p = Db::name('salesman_price')->where('salesman_id',$v['manage_id'])->where('type_pk_id',$vo1['goods_id'])->where('type',$vo['order_type'])->value('per')/100;
                                if(is_null($s_p)) $s_p = 0;
                                //$alipaySevice->writeLog('1single_per'.$v['manage_id'].'-'.$single_per);
                                $single_per += $vo1['price']*$vo1['number']*$s_p;//价钱*数量*分销比 独立销售比例

                            }
                        }
                        if($vo['order_type'] == 2){ //礼包
                            $s_p = Db::name('salesman_price')->where('salesman_id',$v['manage_id'])->where('type_pk_id',$order['single_id'])->where('type',$vo['order_type'])->value('per')/100;
                            if(is_null($s_p)) $s_p = 0;
                            $single_per = $order['amount']*$s_p;
                        }
                        if($vo['order_type'] == 3){ //礼包
                            $s_p = Db::name('salesman_price')->where('salesman_id',$v['manage_id'])->where('type_pk_id',$order['gift_id'])->where('type',$vo['order_type'])->value('per')/100;
                            if(is_null($s_p)) $s_p = 0;
                            //$alipaySevice->writeLog('3sp'.$s_p);
                            //$alipaySevice->writeLog('3single_per'.$single_per);
                            $single_per = $order['amount']*$s_p;
                        }
                    }
                    $insert = [
                        'order_number' => $order['order_number'],
                        'manage_id' => $v['manage_id'],
                        'order_type' => $order['order_type'],
                        'member_id' => $order['member_id'],
                        'amount' => $amount,
                        'price' => $amount*$pre/100,
                        'per' => $amount*$zhishu/100,
                        'single_per' => $single_per,
                        'create_time' => $order['create_time']
                    ];
                    Db::name('member_distribution_records')->insert($insert);
                }
            }
        }
    }
}